<?php

namespace App\Http\Controllers\Develop;

use Carbon\Carbon;

use Illuminate\Http\Request;

use App\Models\CheckoutConfig;

use App\Models\Pay\SysPaytype;

use App\Models\Send\SysSendType;

use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Redis as Redis;

class CheckoutController extends Controller
{

    /**
     * 结算配置列表页
     * @param $config_id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function index( $config_id )
    {

        if ( !ebsig_is_int( $config_id ) ) {
            return response()->json(['code' => 10000, 'message' => '配置id不存在']);
        }

        $send_type = SysSendType::select('sendTypeID','sendTypeName')->get()->toArray();

        $pay_type = SysPaytype::select('payTypeID','payTypeName')->get()->toArray();

        $config_array = CheckoutConfig::find( $config_id )->toArray();

        $data['send_type'] = $send_type;
        $data['pay_type'] = $pay_type;
        $data['config_array'] = $config_array;
        $data['config_id'] = $config_id;

        return view ('develop/checkout/common' ,$data );

    }

    /**
     * 结算配置信息保存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store( Request $request )
    {

        if( !isset( $request['config_id'] ) || !ebsig_is_int( $request['config_id'] ) ) {
            return response()->json(['code' => 10000, 'message' => '配置id不存在']);
        }

        //校验参数
        if( !isset( $request['config_name'] ) && empty( $request['config_name'] )){
            return response()->json(['code' => 10001, 'message' => '配置名称不能为空']);
        }

        if( !isset( $request['send_type'] ) && empty( $request['send_type'] )){
            return response()->json(['code' => 10002, 'message' => '配送方式不能为空']);
        }

        if( !isset( $request['pay_type'] ) && empty( $request['pay_type'] )){
            return response()->json(['code' => 10003, 'message' => '支付方式不能为空']);
        }

        $pay_type_id_array = $request['pay_type'];
        $send_type_id_array = $request['send_type'];
        $pay_type_name_array = array();
        $send_type_name_array = array();

        //处理支付方式
        foreach( $pay_type_id_array as $key=>$pay_type_id ) {
            $pay_type_array = SysPaytype::select('payTypeID','payTypeName')->find( $pay_type_id )->toArray();
            if ( $pay_type_array ) {
                $pay_type_name_array[] = $pay_type_array['payTypeName'];
            } else {
                unset($pay_type_id_array[$key]);
            }
        }

        //处理配送方式
        foreach( $send_type_id_array as $key => $send_type_id ) {
            $send_type_array = SysSendType::select('sendTypeID','sendTypeName')->find( $send_type_id )->toArray();
            if ( $send_type_array ) {
                $send_type_name_array[] = $send_type_array['sendTypeName'];
            } else {
                unset($send_type_id_array[$key]);
            }
        }

        //查询配置是否存在
        $sysBillConfig = CheckoutConfig::find( $request['config_id'] );
        if ( !$sysBillConfig ) {
            return response()->json(['code' => 10004, 'message' => '配置信息没有找到']);
        }
        $sysBillConfig->member_flg = 3;
        $sysBillConfig->timeStamp = Carbon::now();
        $sysBillConfig->creator = Redis::get('WDH_ADMIN_USER_ID_' . session_id());
        $sysBillConfig->createTime = Carbon::now();
        $sysBillConfig->config_name = $request['config_name'];
        $sysBillConfig->send_type = implode(',',$send_type_id_array);
        $sysBillConfig->pay_type  = implode(',',$pay_type_id_array);
        $sysBillConfig->send_type_name = implode('、',$send_type_name_array);
        $sysBillConfig->pay_type_name  = implode('、',$pay_type_name_array);
        $sysBillConfig->is_balance_password = $request['is_balance_password'];
        $sysBillConfig->is_points_password = $request['is_points_password'];
        $sysBillConfig->is_show_invoice = $request['is_show_invoice'];
        $sysBillConfig->is_show_coupon = $request['is_show_coupon'];
        $sysBillConfig->is_show_remark = $request['is_show_remark'];
        $sysBillConfig->is_show_yzm = $request['is_show_yzm'];
        $sysBillConfig->is_show_pickup_time = isset($request['is_show_pickup_time'])?$request['is_show_pickup_time']:0;
        $sysBillConfig->remark = isset($request['remark'])?$request['remark']:'';
        $sysBillConfig -> mall_range_type = isset($request['mall_range_type']) ? $request['mall_range_type'] : 0;
        $sysBillConfig -> pick_up_goods_conditions = isset($request['pick_up_goods_conditions']) ? implode(',',$request['pick_up_goods_conditions']) : '';
        $sysBillConfig->freight_free = $request['freight_free'];

        $sysBillConfig->save();

        return response()->json(['code' => 200, 'message' => '保存成功']);

    }


}